﻿using FubarDev.FtpServer;
using FubarDev.FtpServer.AccountManagement;
using FubarDev.FtpServer.FileSystem.DotNet;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FTPService
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        IFtpServerHost ftpServerHost;
        private async void button1_Click(object sender, EventArgs e)
        {
            // 设置依赖项注入
            var services = new ServiceCollection();

            // 使用%TEMP%/TestFtpServer作为根文件夹
            services.Configure<DotNetFileSystemOptions>(opt => opt
                .RootPath = @"D:\\Hangs\\Package\\B853");

            //Path.Combine(Path.GetTempPath(), "TestFtpServer"));

            // 添加FTP服务器服务
            // DotNetFileSystemProvider = 使用.NET文件系统功能
            // AnonymousMembershipProvider = 仅允许匿名登录
            services.AddFtpServer(builder =>
            {
                builder.UseDotNetFileSystem(); // 使用.NET文件系统功能
                //builder.EnableAnonymousAuthentication();// 允许匿名登录

                builder.Services.AddSingleton<IMembershipProvider, TestMembershipProvider>();//用户登录
            });

            // 配置FTP服务器
            services.Configure<FtpServerOptions>(opt => opt.ServerAddress = "127.0.0.1");

            // 构建服务提供商
            var serviceProvider = services.BuildServiceProvider();

            // 初始化FTP服务器
            ftpServerHost = serviceProvider.GetRequiredService<IFtpServerHost>();

            // 启动FTP服务器
            await ftpServerHost.StartAsync(CancellationToken.None);


            richTextBox1.AppendText("打开ftp服务");


            //FtpServer server = new FtpServer();
            //await server.StartAsync();
        }

        public class TestMembershipProvider : IMembershipProvider
        {

            public Task<MemberValidationResult> ValidateUserAsync(string username, string password)
            {
                if (username == "1" && password == "1")
                {
                    var identity = new ClaimsIdentity();
                    identity.AddClaim(new Claim(ClaimTypes.Name, username));
                    identity.AddClaim(new Claim(ClaimTypes.Role, "admin"));
                    return Task.FromResult(new MemberValidationResult(MemberValidationStatus.AuthenticatedUser, new ClaimsPrincipal(identity)));

                }

                return Task.FromResult(new MemberValidationResult(MemberValidationStatus.InvalidLogin));
            }
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            DataTable Result = new DataTable();
            Result.Rows.Clear();
            Result.Columns.Clear();
            Result.Clear();

            Result.Columns.Add("Index", typeof(int));
            Result.Columns.Add("CmdSuitsFileName", typeof(string));
            Result.Columns.Add("CmdTag", typeof(string));
            Result.Columns.Add("Cmd", typeof(string));
            Result.Columns.Add("Result", typeof(string));

            Result.Rows.Add();
            Result.Rows[0][1] = "df";
            
        }
    }   
}
